import time

from common.Utils import FSDriver
from selenium.webdriver.common.by import By


# 注册界面
class EnrollPage:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:58080/sign-up.html"
        self.driver = FSDriver.driver
        self.driver.get(self.url)
        self.driver.implicitly_wait(5)

    # 注册输入框清空
    def input_clear(self):
        # 是否到该页面
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")

        # 清空输入框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()

    # 点击登录按钮返回登录界面
    def return_login(self):
        self.driver.find_element(By.XPATH, '/html/body/div/div/div[2]/a').click()

    # 成功注册
    def succeed_enroll_test(self):
        self.input_clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("mo")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(4) > div > span").click()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(5) > div > span").click()
        FSDriver.getscreeshot()

        # 判断是否勾选
        # element = self.driver.find_element(By.XPATH, '//*[@id="policy"]').get_attribute("class")

        enroll_url = self.driver.current_url
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > h2")

        # 注册成功会跳转到登录页面
        assert enroll_url == self.driver.current_url

    # 输入框为空
    def null_input(self):
        self.input_clear()

        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        FSDriver.getscreeshot()
        assert self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(6) > label > span > a")

    # 注册填写违规
    def fail_user_name_pass_conpass(self):
        self.input_clear()

        val_url = self.driver.current_url
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("毕书尽")

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        FSDriver.getscreeshot()

        # 勾选协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        if self.driver.current_url == val_url:
            print("注册失败，请重试")


        assert self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(6) > label > span > a")

    # 用户名违规
    def fail_user(self):
        self.input_clear()

        val_url = self.driver.current_url
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("毕书尽")

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        FSDriver.getscreeshot()

        # 勾选协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        if self.driver.current_url == val_url:
            print("注册失败，请重试")

        assert self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(6) > label > span > a")

    # 昵称违规
    def fail_name(self):
        self.input_clear()

        val_url = self.driver.current_url
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("毕书尽")

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        FSDriver.getscreeshot()

        # 勾选协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        if self.driver.current_url == val_url:
            print("注册失败，请重试")

        assert self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(6) > label > span > a")

    # 密码与确认密码不符
    def fail_pass_conpass(self):
        self.input_clear()

        val_url = self.driver.current_url
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234567")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123456789")

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(4) > div > span").click()
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(5) > div > span").click()
        FSDriver.getscreeshot()

        # 勾选协议
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        if self.driver.current_url == val_url:
            print("注册失败，请重试")

        assert self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(6) > label > span > a")

    # 注册失败
    def fail_enroll_test(self):
        self.driver.maximize_window()
        self.null_input()
        self.fail_user_name_pass_conpass()
        self.fail_user()
        self.fail_name()
        self.fail_pass_conpass()




